package com.mallbook.controller.filter;

import com.mallbook.model.pojo.Admin;
import com.mallbook.model.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-07-08
 */
@WebFilter("/admin/*")
public class AdminFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(AdminFilter.class);

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        logger.info("进入AdminFilter过滤器");
        HttpServletRequest req = (HttpServletRequest) request;
        logger.info("URL:" + req.getRequestURL());
        logger.info("QS:" + req.getQueryString());
        HttpServletResponse resp = (HttpServletResponse) response;
        HttpSession session = req.getSession();
        Admin admin = (Admin) session.getAttribute("ADMIN");
        if (admin == null) {
            resp.sendRedirect("/adminLogin"); // 转到登录界面
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
    }

}
